// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { videoAds } = require("../../ads/video");
const { show } = require("../../game/alert");
const {  PrefabCache, prefabPool, getPrefabPool } = require("../../util/gameResources");
const { shopData, player, adPosition } = require("../gameData");
cc.Class({
    extends: cc.Component,

    properties: {
        menuList:cc.Node,
        count: cc.EditBox,
    },

    // LIFE-CYCLE CALLBACKS:

    onEnable () {
            
            shop.foodList.forEach(val=>{
                if(val.state == 1){
                    //菜单生成
                    let item  = getPrefabPool('stockItem');
                    this.menuList.addChild(item);
                    item.getChildByName("foodName").getComponent(cc.Label).string = `${val.name}`
                    item.getChildByName("sales").getComponent(cc.Label).string = `销量：${formatMoney(val.sales)}`
                    item.getChildByName("transport").getComponent(cc.Label).string = `配送：${formatMoney(val.transport)}`
                    item.getChildByName("price").getComponent(cc.Label).string = `价格：${formatMoney(val.price * 0.2)}`
                    item.getChildByName("stock").getComponent(cc.Label).string = `库存:${formatMoney(val.stock)}`
                    //合成表
                    item.id = val.id;
                    item.cost = val.price <= 0? 1: val.price;
                    this.menuList.height += 122;
                }else{
                    // let item  = cc.instantiate(PrefabCache['stockItem']);
                    // this.menuList.addChild(item);
                    // item.getChildByName("price").getComponent(cc.Label).string = `知名度：${val.name}  店铺等级：${val.price}`
                }
            })
    },
    onDisable () {
        this.menuList.destroyAllChildren();
        this.menuList.height = 0;

    },
    start () {

    },
    buyAll() {
        if(!adPosition.oneKey) {
            show("未来广告","是否观看广告解锁",res => {
                if(res) {
                    videoAds.showAd(reward => {
                        if(reward){
                            adPosition.oneKey = true;
                        }
                    })
                }
            })
            return ;
        }
        let buyCount = parseInt(this.count.string);
        if(!buyCount || buyCount <= 0) {
            buyCount = 100;
        }
        let count = 0;
        shop.foodList.forEach(val => {
            count += val.price * 0.2 * buyCount;
        })
        if(shop.money < count){
            show('未来批发',"金钱不足");
            return ;
        }
        shop.money -= count;
        shop.foodList.forEach(val => {
            shop.transportList.push({id: val.id, name: val.name, stock: buyCount, transport: val.level * 60});
        })
        show('未来批发',"货物已开始配送");
    },
    changeActive(target, i){
        this.node.active = !this.node.active;
    }
    // update (dt) {},
});
